package com.zekai.calculator;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class HistoryActivity extends AppCompatActivity {


    private static final String Tag="Calculator-HistoryActivity";
    private static  MainViewModel viewModel;
    public static void setViewModel(MainViewModel viewModel){
        HistoryActivity.viewModel=viewModel;
    }
    private HistoryRepository historyRepository;
    private final ActivityResultLauncher<Intent> helpLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == RESULT_OK) {
                    // 处理返回的结果
                    Intent data = result.getData();
                    if (data != null) {

                    }
                }
            }
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        HistoryRepository historyRepository = HistoryRepository.getInstance();

        viewModel.setExpression(historyRepository.getAllExpressions());
        viewModel.setResult(historyRepository.getAllResults());
        Log.d(Tag,"onCreate");
        setContentView(R.layout.activity_history);
        // 获取表达式和结果列表
        List<String> expressions = viewModel.getExpressions();
        List<String> results = viewModel.getResults();
        // 创建 RecyclerView 和适配器
        RecyclerView recyclerView = findViewById(R.id.historyRecyclerView);
        HistoryAdapter adapter = new HistoryAdapter(expressions, results, helpLauncher);

        // 设置 RecyclerView 的布局管理器和适配器
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }
    protected void onStart(){
        super.onStart();
        Log.d(Tag,"onStart");
    }
    protected void onResume(){
        super.onResume();
        Log.d(Tag,"onResume");
    }
    protected void onPause(){
        super.onPause();
        Log.d(Tag,"onPause");
    }
    protected void onStop(){
        super.onStop();
        Log.d(Tag,"onStop");
    }
    protected void onDestroy(){
        super.onDestroy();
        Log.d(Tag,"onDestroy");
    }
    protected void onRestart(){
        super.onRestart();
        Log.d(Tag,"onRestart");
    }
}
